package com.ewanghuiju.app.ui.message.activity;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RootActivity;
import com.ewanghuiju.app.base.contract.message.MessageListContract;
import com.ewanghuiju.app.model.bean.local.MessageCommonBean;
import com.ewanghuiju.app.presenter.message.MessageListPresenter;
import com.ewanghuiju.app.ui.message.adapter.MessageListAdapter;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 消息列表
 * @time 2020/2/24
 */
public class MessageListActivity extends RootActivity<MessageListPresenter> implements MessageListContract.View {

    @BindView(R.id.view_main)
    RecyclerView recyclerViewMsg;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private int msgType;//1; //系统消息 2; //活动消息  3; //个人消息 4; //新手

    private MessageListAdapter messageListAdapter;

    private int currentPage = 1;


    @Override
    protected int getLayout() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        msgType = getIntent().getIntExtra(Constants.MESSAGE_TYPE, Constants.SETTING_MESSAGE);
        if (msgType == Constants.ACTIVITY_MESSAGE) {

        } else if (msgType == Constants.NEWCOURSE_MESSAGE) {
            //新手教程
            tvTitle.setText(getString(R.string.home_newborn_jc));
        }
        initMessageList();
        initRefreshLayout();
    }

    private void initRefreshLayout() {
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getData(currentPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData(currentPage);
            }
        });
        //首次进入显示
        LoadingDialogUtils.show(mContext);
        currentPage = 1;
        mPresenter.getData(currentPage);
    }


    /**
     * 消息列表
     */
    private void initMessageList() {
        if (msgType == Constants.ACTIVITY_MESSAGE) {
            messageListAdapter = new MessageListAdapter(R.layout.item_activity_message_list);
        } else {
            messageListAdapter = new MessageListAdapter(R.layout.item_message_list);
        }
        messageListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageCommonBean messageCommonBean = (MessageCommonBean) adapter.getItem(position);
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_NEW + messageCommonBean.getId())
                        .putExtra(Constants.WEBURL_TITLE, "新手教程").startActivity(true);
            }
        });
        recyclerViewMsg.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerViewMsg.setAdapter(messageListAdapter);
//        recyclerViewMsg.setHasFixedSize(true);
//        recyclerViewMsg.setNestedScrollingEnabled(false);
//        messageListAdapter.notifyDataSetChanged();
    }

//    @Override
//    protected int getTitleIntText() {
//        return R.string.setting_message;
//    }


    @Override
    protected void intSmartRefreshLayout() {
//        super.intSmartRefreshLayout();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .transparentBar()
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(List<MessageCommonBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            messageListAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            messageListAdapter.addData(list);
        }

        if (messageListAdapter.getData().size() > 0) {
            stateMain();
        } else {
            stateEmpty();
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (messageListAdapter.getData().size() == 0) {
            super.stateError();
        }
    }
}
